package com.yc.yc112plyqnzf.controller;

import com.yc.yc112plyqnzf.vo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


@RestController
public class IndexDo {

    @Value("${uploadDir}")
    private String uploadDir;

    @PostMapping("upload")
    Result  upload(@RequestParam("file")MultipartFile file) throws IOException {
        File dir = new File(uploadDir.substring("file:/".length()));
        String filename = UUID.randomUUID().toString().replaceAll("-", "");
        String originalFilename = file.getOriginalFilename();
        String suffix=originalFilename.substring(originalFilename.lastIndexOf("."));
        filename+=suffix;
        File fileobj =new File(dir,filename);
            file.transferTo(fileobj);
        String webpath="/upload/" +filename;
        return new Result(1,"文件上传成功",webpath);
    }
}
